package com.shop.tools;

import com.shop.dto.Email;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * com.shop.tools文档说明
 *
 * @author Angluzha
 * @version 2022/9/20
 */
public class MessageTool {
    public static void sendEmail(Email email) throws Exception {
        Properties properties = new Properties();
        properties.load(MessageTool.class.getClassLoader().getResourceAsStream("Message.properties"));
        Session session = Session.getInstance(properties);
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        Transport transport = session.getTransport();
        transport.connect("smtp.163.com",email.getSender(),"CCDREJXEHDEURLBH");//
        //设置发件人
        message.setSender(new InternetAddress(email.getSender()));
        //设置收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(email.getAddressee()));
        //设置抄送人
        message.setRecipient(Message.RecipientType.CC,new InternetAddress(email.getSender()));
        //设置邮件标题
        message.setSubject(email.getTitle());
        //设置邮件内容
        message.setContent(email.getContentText(),"text/html;charset=UTF-8");
        //发送邮件
        transport.sendMessage(message,message.getAllRecipients());
        //关闭接口
        transport.close();
    }
}
